package com.xhui.vimarket.zkplus.zul.attr.util;

import java.util.ArrayList;
import java.util.List;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.Composer;

import com.xhui.vimarket.common.utils.vo.RetVO;
import com.xhui.vimarket.zkplus.utils.ComposerUtil;
import com.xhui.vimarket.zkplus.zul.attr.AttrExt;
import com.xhui.vimarket.zkplus.zul.attr.bean.AttrInst;
import com.xhui.vimarket.zkplus.zul.attr.bus.AttrBus;
import com.xhui.vimarket.zkplus.zul.attr.bus.IAttrBusDextComposer;
import com.xhui.vimarket.zkplus.zul.attr.bus.IAttrBusDivComposer;
import com.xhui.vimarket.zkplus.zul.attr.bus.impl.AttrBusImpl;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.util.AttrUtl.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-14
 * @功能说明：属性控件
 * @修改记录： =============================================================<br>
 *        日期:2010-12-14 创建
 *        =============================================================<br>
 */
public final class AttrUtil {
    
    /**
     * 构造函数. 
     */
    private AttrUtil() {
    }
    
    /**
     * 比较最终数据.
     * @param oldAttrInstList List<AttrInst>旧值
     * @param newAttrInstList List<AttrInst>新值
     * @return List<AttrInst>
     */
    public static List<AttrInst> compareAttrInst(final List<AttrInst> oldAttrInstList,
        final List<AttrInst> newAttrInstList) {
        final List<AttrInst> compareInstList = new ArrayList<AttrInst>();
        final int oldsum = oldAttrInstList == null ? 0
            : oldAttrInstList.size();
        final int newsum = newAttrInstList == null ? 0
            : newAttrInstList.size();
        // 界面未显示的值过滤，避免多删除 TODO
        
        // 比较删除的
        for (int oi = 0; oi < newsum; oi++) {
            final AttrInst oldAttrInst = oldAttrInstList.get(oi);
            int ni = 0;
            for (; ni < oldsum; ni++) {
                final AttrInst newAttrInst = newAttrInstList.get(ni);
                if (oldAttrInst.getAttrId() == newAttrInst.getAttrId()) {
                    break; // 找到 
                }
            }
            if (ni >= newsum) { // 未碰到相同的，表明删除了
                compareInstList.add(oldAttrInst);
            }
        }
        return compareInstList;
    }
    
    /**
     * 比较出删除数据.
     * @param oldAttrInstList List<AttrInst>旧值
     * @param newAttrInstList List<AttrInst>新值
     * @return List<AttrInst>
     */
    public static List<AttrInst> compareDelAttrInst(final List<AttrInst> oldAttrInstList,
        final List<AttrInst> newAttrInstList) {
        final List<AttrInst> compareInstList = new ArrayList<AttrInst>();
        final int oldsum = oldAttrInstList == null ? 0
            : oldAttrInstList.size();
        final int newsum = newAttrInstList == null ? 0
            : newAttrInstList.size();
        // 界面未显示的值过滤，避免多删除 TODO
        
        // 比较删除的
        for (int oi = 0; oi < oldsum; oi++) {
            final AttrInst oldAttrInst = oldAttrInstList.get(oi);
            int ni = 0;
            for (; ni < newsum; ni++) {
                final AttrInst newAttrInst = newAttrInstList.get(ni);
                if (oldAttrInst.getAttrId() == newAttrInst.getAttrId()) {
                    break; // 找到 
                }
            }
            if (ni >= newsum) { // 未碰到相同的，表明删除了
                compareInstList.add(oldAttrInst);
            }
        }
        return compareInstList;
    }
    
    /**
     * 如果入参是null或者"",用另一入参rpt替代入参返回，否则返回入参的trim().
     * 
     * @param str
     *            String
     * @param rpt
     *            String
     * @return String
     * @author: chenjun
     * @修改记录： ==============================================================<br>
     *        日期:2007-9-21 chenjun 创建方法，并实现其功能
     *        ==============================================================<br>
     */
    public static String strnull(final String str, final String rpt) {
        if (str == null || str.equals("null") || str.equals("") || str.trim() == null) {
            return rpt;
        } else {
            return str.trim();
        }
    }
    
    /**
     * tranRetVoToOneList.
     * 
     * @param retVo RetVO
     * @return List<RetVO>
     * @author wuyx
     * 2011-3-23 wuyx 
     */
    public static List<RetVO> tranRetVoToOneList(RetVO retVo) {
        List<RetVO> list = new ArrayList<RetVO>();
        list.add(retVo);
        return list;
    }
    
    /**
     * 获取属性服务总线.
     * 
     * @param comp Component
     * @return AttrBus
     * @author wuyx
     * 2012-1-16 wuyx 
     */
    public static AttrBus getAttrBus(Component comp) {
        Component attrExtComp = ComposerUtil.getSupExt(comp, AttrExt.class);
        if (attrExtComp != null) {
            AttrExt attrExt = (AttrExt) attrExtComp;
            if (attrExt.getAttrBus() != null) {
                return attrExt.getAttrBus();
            }
        }
        //xhuip
        return null;
    }
    
    /**
     * setAttrBusDivComposer.
     * 
     * @param comp Component
     * @param attrBusDivComposer AttrBusDivComposer
     * @author wuyx
     * 2012-1-16 wuyx 
     */
    public static void setAttrBusDivComposer(final Component comp,
        IAttrBusDivComposer attrBusDivComposer) {
        Composer composer = ComposerUtil.getComposer(comp);
        if (composer instanceof IAttrBusDextComposer) {
            ((IAttrBusDextComposer) composer).setAttrBusDivComposer(attrBusDivComposer);
        }
    }
    
}
